package com.linux.huhx.thread4.synchronized_1;

/**
 * @Author: huhx
 * @Date: 2017-11-08 下午 10:22
 */
public class SynchronizedDemo_1 {
    // share variable
    private boolean ready = false;
    private int result = 0;
    private int number = 1;

    public void write() {
        ready = true;
        number = 2;
    }

    public void read() {
        if (ready) {
            result = number * 3;
        }
        System.out.println("result = " + result);
    }

    // inner thread
    private class ReadWriteThread extends Thread {
        private boolean flag;

        public ReadWriteThread(boolean flag) {
            this.flag = flag;
        }

        @Override
        public void run() {
            if (flag) {
                write();
            } else {
                read();
            }
        }
    }

    public static void main(String[] args) {
        SynchronizedDemo_1 synchronizedDemo1 = new SynchronizedDemo_1();

        synchronizedDemo1.new ReadWriteThread(true).start();
        synchronizedDemo1.new ReadWriteThread(false).start();
    }
}
